package com.aps.server.controller;

import com.aps.common.entity.Result;
import com.aps.server.entity.ApsOperation;
import com.aps.server.entity.ApsOrder;
import com.aps.server.service.ApsOperationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工序表 前端控制器
 * 
 * @author aps
 * @since 2024-01-01
 */
@Slf4j
@Tag(name = "工序管理", description = "工序相关接口")
@RestController
@RequestMapping("/api/operation")
@CrossOrigin
public class ApsOperationController {

    @Autowired
    private ApsOperationService apsOperationService;

    @Operation(summary = "根据工艺获取工序列表", description = "分页查询工序列表")
    @GetMapping("/query-operation-by-craft")
    public Result<List<ApsOperation>> queryOperationByCraftCode(
            @RequestParam(value = "craftId" , required = true) String craftId ,
            @RequestParam(value = "operationCode" , required = false) String operationCode ,
            @RequestParam(value = "operationName" , required = false) String operationName
            ) {
        Result<List<ApsOperation>> result = new Result<>();
        try {
            List<ApsOperation> s = apsOperationService.getOperationsByTenantId(craftId,operationCode,operationName);
            result.setData(s);
            result.setSuccess(true);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("查询工序数据失败");
        }
        return result;
    }


    @Operation(summary = "新增工序", description = "新增工序信息")
    @PostMapping("/create")
    public Result<String> createOperation(@RequestBody ApsOperation apsOperation) {
        // TODO: 实现新增逻辑
        if(apsOperation.getOperationCode() == null){
            return Result.error(400, "工序信息创建失败,缺少工艺编码");
        }
        try {
            boolean success = apsOperationService.createOperation(apsOperation);
            if (success) {
                return Result.success(apsOperation.getId().toString(), "工序信息创建成功");
            } else {
                return Result.error(400, "工序信息创建失败");
            }
        } catch (Exception e) {
            log.error("创建工序信息失败", e);
            return Result.error(500, "创建工序信息失败: " + e.getMessage());
        }
    }


    @PostMapping("/modify")
    @Operation(summary = "更新工序", description = "更新工序信息")
    public Object updateOperation(@RequestBody ApsOperation apsOperation) {
        try {
            String id = apsOperation.getId();
            if(id == null) {
                return Result.error(400, "数据错误","未获取到数据id！");
            }
            apsOperation.setId(id);
            boolean success = apsOperationService.updateOperation(apsOperation);
            if (success) {
                return Result.success("工序信息更新成功");
            } else {
                return Result.error(400, "工序信息更新失败");
            }
        } catch (Exception e) {
            log.error("更新工序信息失败", e);
            return Result.error(500, "更新工序信息失败: " + e.getMessage());
        }
    }

    @Operation(summary = "删除工序", description = "根据ID删除工序")
    @GetMapping("/del/{id}")
    public Object delete(@PathVariable String id) {
        try {
            boolean success = apsOperationService.deleteOperation(id);
            if (success) {
                return Result.success("工序信息删除成功");
            } else {
                return Result.error(400, "工序信息删除失败");
            }
        } catch (Exception e) {
            log.error("删除工序信息失败", e);
            return Result.error(500, "删除工序信息失败: " + e.getMessage());
        }
    }
}
